# == Coding: UTF-8 ==
# @Project :        BooksManagementSystem
# @fileName         RandomlyGenerateBookInformation.py  
# @version          v0.1
# @author           Echo
# @GiteeWarehouse   https://gitee.com/liu-long068/
# @editsession      2023/6/6
# @Software:        PyCharm
# ====/******/=====
""" 生成图书信息 """
import json

import requests
from lxml import etree

""" 书名，作者，出版社，出版年份，ISBN,价格,图书馆库存"""


class GenerateBookInformation:
    def __init__(self):
        self.home_urls = ['https://book.douban.com/top250?start={}'.format(str(i)) for i in range(0, 250, 25)]
        self.header = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                          'Chrome/114.0.0.0 Safari/537.36'
        }

    def get_book_url(self, url):
        """ 获取图书链接 """
        html = requests.get(url, headers=self.header)
        selector = etree.HTML(html.text)
        infos = selector.xpath('//tr[@class="item"]')
        book_urls = []
        for info in infos:
            # book_name = '《' + info.xpath('td/div/a/@title')[0] + '》'
            # print(book_name)
            book_url = info.xpath('td/div/a/@href')[0]  # 书本链接
            book_urls.append(book_url)
        return book_urls
        # book_infos = info.xpath('td/p/text()')[0]  # 书本作者、出版社、出版日期、价格信息
        # author = book_infos.split('/')[0]  # 作者
        # publisher = book_infos.split('/')[-3]  # 出版社
        # publisherTime = book_infos.split('/')[-2]  # 出版时间
        # price = book_infos.split('/')[-1]  # 书本价格

    def get_book_name(self, url):
        """ 获取书名 """
        html = requests.get(url, headers=self.header)
        selector = etree.HTML(html.text)
        book_name = selector.xpath('//*[@id="wrapper"]/h1/span')[0]
        return book_name

    def get_book_author(self, url):
        """ 获取图书作者 """
        html = requests.get(url, headers=self.header)
        selector = etree.HTML(html.text)
        book_author = selector.xpath('//*[@id="info"]/a[1]')[0]
        return book_author

    def get_book_publisher(self, url):
        """ 获取图书出版社 """
        html = requests.get(url, headers=self.header)
        selector = etree.HTML(html.text)
        book_publisher = selector.xpath('//*[@id="info"]/a[1]')[0]
        return book_publisher

    def get_book_publisherTime(self, url):
        """ 获取图书出版时间 """
        html = requests.get(url, headers=self.header)
        selector = etree.HTML(html.text)
        book_publisherTime = selector.xpath('//*[@id="info"]/text()[1]')[0]
        return book_publisherTime

    def get_book_ISBN(self, url):
        """ 获取图书ISBN """
        html = requests.get(url, headers=self.header)
        selector = etree.HTML(html.text)
        book_ISBN = selector.xpath('//*[@id="info"]/text()[6]')[0]
        return book_ISBN

    def get_book_price(self, url):
        """ 获取图书价格 """
        html = requests.get(url, headers=self.header)
        selector = etree.HTML(html.text)
        book_price = selector.xpath('//*[@id="info"]/text()[3]')[0]
        return book_price

    def book_inventory(self):
        """ 图书库存 """
        return int(10)  # 默认每本书库存10本


if __name__ == '__main__':
    generate_book = GenerateBookInformation()
    books = []
    books_information = {}
    for home_url in generate_book.home_urls:
        for book_url in generate_book.get_book_url(home_url):
    #         books_information['书名'] = generate_book.get_book_name(book_url)
    #         books_information['作者'] = generate_book.get_book_author(book_url)
    #         books_information['出版社'] = generate_book.get_book_publisher(book_url)
    #         books_information['出版年份'] = generate_book.get_book_publisherTime(book_url)
    #         books_information['ISBN'] = generate_book.get_book_ISBN(book_url)
    #         books_information['价格'] = generate_book.get_book_price(book_url)
    #         books_information['现库存'] = generate_book.book_inventory()
    #         books.append(books_information)
    # with open('../books/boos.json', 'w', encoding='utf-8') as file:
    #     json.dump(books, file, ensure_ascii=False, indent=4)
    # print("写入完成。")
            print(generate_book.get_book_author(book_url))
